package cn.jmz.micpro.service.impl;

import cn.jmz.micpro.common.enums.BaseEnum;
import cn.jmz.micpro.common.enums.user.UserEnum;
import cn.jmz.micpro.common.result.GlobalResult;
import cn.jmz.micpro.entity.db.user.User;
import cn.jmz.micpro.entity.dto.user.UserBaseLogin;
import cn.jmz.micpro.entity.dto.user.UserMobileLogin;
import cn.jmz.micpro.jpa.user.UserRepository;
import cn.jmz.micpro.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * cn.jmz.micpro.service.impl
 *
 * @author JMZ
 * @date 2021/12/16 16:36
 */
@Service
public class UserService implements IUserService {

    private UserRepository userRepository;

    @Autowired
    public UserService(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    @Override
    public GlobalResult baseLogin(UserBaseLogin userBaseLogin) {
        User user = userRepository.findByUserName(userBaseLogin.getUserName());
        //用户不存在或者密码错误
        if(Objects.isNull(user) || !userBaseLogin.getPassWord().equals(user.getUserPassword())){
            return new GlobalResult(UserEnum.LOGIN_FAIL.getCode(),UserEnum.LOGIN_FAIL.getMsg());
        }
        return new GlobalResult(BaseEnum.SUCCESS.getCode(),BaseEnum.SUCCESS.getMsg(),user);
    }

    @Override
    public GlobalResult mobileLogin(UserMobileLogin userMobileLogin) {
        //优先判断验证码
        if(!"1234".equals(userMobileLogin.getSmsCode())){
            return new GlobalResult(UserEnum.SMS_ERROR.getCode(),UserEnum.SMS_ERROR.getMsg());
        }
        //判断是否存在该用户
        User user = userRepository.findByUserMobile(userMobileLogin.getUserMobile());
        if(Objects.isNull(user)){
            return new GlobalResult(UserEnum.MOBILE_NOT_EXIST.getCode(),UserEnum.MOBILE_NOT_EXIST.getMsg());
        }
        return new GlobalResult(BaseEnum.SUCCESS.getCode(),BaseEnum.SUCCESS.getMsg(),user);
    }

    @Override
    public GlobalResult register() {
        return null;
    }
}
